#include "bottomstatuswidget.h"
#include <QPushButton>

BottomStatusWidget::BottomStatusWidget(QWidget *parent) : QWidget(parent)
{
    // 创建水平布局
    layout = new QHBoxLayout(this);
    layout->setContentsMargins(10, 5, 10, 5);
    layout->setSpacing(5);  // 减小间距使按钮更紧密
    
    // 创建通道按钮组
    channelButtonGroup = new QButtonGroup(this);
    
    // 创建状态图标
    muteCheckBox = new QCheckBox("静音", this);
    fullScreenCheckBox = new QCheckBox("全屏", this);
    alarmCheckBox = new QCheckBox("报警", this);
    
    // 设置状态图标样式
    QString checkBoxStyle = "QCheckBox { spacing: 5px; }"
                           "QCheckBox::indicator { width: 16px; height: 16px; }";
    muteCheckBox->setStyleSheet(checkBoxStyle);
    fullScreenCheckBox->setStyleSheet(checkBoxStyle);
    alarmCheckBox->setStyleSheet(checkBoxStyle);
    
    // 添加到布局
    layout->addStretch(); // 添加左侧弹性空间
    
    // 创建通道按钮的容器布局，使它们紧密排列
    QHBoxLayout *channelLayout = new QHBoxLayout();
    channelLayout->setSpacing(2);  // 通道按钮之间的间距更小
    channelLayout->setContentsMargins(0, 0, 0, 0);
    
    // 添加默认通道按钮
    QStringList defaultChannels = {"1", "4", "9", "16"};
    
    // 创建新按钮并添加到通道布局
    foreach (QString channel, defaultChannels) {
        QPushButton *btn = new QPushButton(channel, this);
        btn->setFixedSize(60, 25);
        btn->setStyleSheet("QPushButton { background-color: #f0f0f0; border: 1px solid #cccccc; }"
                          "QPushButton:checked { background-color: #d0d0d0; font-weight: bold; }");
        btn->setCheckable(true);
        
        // 默认选中第一个通道
        if (channel == defaultChannels.first()) {
            btn->setChecked(true);
        }
        
        // 添加到按钮组和通道布局
        channelButtonGroup->addButton(btn);
        channelLayout->addWidget(btn);
    }
    
    // 将通道布局添加到主布局
    layout->addLayout(channelLayout);
    
    layout->addStretch(); // 添加右侧弹性空间
    layout->addWidget(muteCheckBox);
    layout->addWidget(fullScreenCheckBox);
    layout->addWidget(alarmCheckBox);
    
    // 设置固定高度
    setFixedHeight(40);
    
    // 连接信号槽
    connect(channelButtonGroup, QOverload<QAbstractButton*>::of(&QButtonGroup::buttonClicked), 
            this, [this](QAbstractButton *button) {
                onChannelButtonClicked(channelButtonGroup->id(button));
            });
    
    connect(muteCheckBox, &QCheckBox::toggled, this, &BottomStatusWidget::onMuteToggled);
    connect(fullScreenCheckBox, &QCheckBox::toggled, this, &BottomStatusWidget::onFullScreenToggled);
}

BottomStatusWidget::~BottomStatusWidget()
{}

void BottomStatusWidget::setChannelButtons(const QStringList &channels)
{
    // 清除现有按钮
    foreach (QAbstractButton *btn, channelButtonGroup->buttons()) {
        channelButtonGroup->removeButton(btn);
        delete btn;
    }
    
    // 创建新按钮
    foreach (QString channel, channels) {
        QPushButton *btn = new QPushButton(channel, this);
        btn->setFixedSize(60, 25);
        btn->setStyleSheet("QPushButton { background-color: #f0f0f0; border: 1px solid #cccccc; }"
                          "QPushButton:checked { background-color: #d0d0d0; font-weight: bold; }");
        btn->setCheckable(true);
        
        // 默认选中第一个通道
        if (channel == channels.first()) {
            btn->setChecked(true);
        }
        
        // 添加到按钮组
        channelButtonGroup->addButton(btn);
        
        // 将按钮添加到布局中，放在弹性空间和状态图标之间
        layout->insertWidget(layout->count() - 4, btn);
    }
}

void BottomStatusWidget::updateStatusIcons(bool mute, bool fullScreen, bool alarm)
{
    // 更新状态图标（如静音、全屏等）
    muteCheckBox->setChecked(mute);
    fullScreenCheckBox->setChecked(fullScreen);
    alarmCheckBox->setChecked(alarm);
    
    // 根据当前状态更新图标样式
    if (mute) {
        muteCheckBox->setStyleSheet("QCheckBox { color: red; }");
    } else {
        muteCheckBox->setStyleSheet("QCheckBox { color: black; }");
    }
    
    if (fullScreen) {
        fullScreenCheckBox->setStyleSheet("QCheckBox { color: red; }");
    } else {
        fullScreenCheckBox->setStyleSheet("QCheckBox { color: black; }");
    }
    
    if (alarm) {
        alarmCheckBox->setStyleSheet("QCheckBox { color: red; }");
    } else {
        alarmCheckBox->setStyleSheet("QCheckBox { color: black; }");
    }
}

bool BottomStatusWidget::isMuted() const
{
    // 获取当前静音状态
    return muteCheckBox->isChecked();
}

bool BottomStatusWidget::isFullScreen() const
{
    // 获取当前全屏状态
    return fullScreenCheckBox->isChecked();
}

bool BottomStatusWidget::hasAlarm() const
{
    // 获取当前警报状态
    return alarmCheckBox->isChecked();
}

void BottomStatusWidget::setCurrentChannel(const QString &channelId)
{
    // 设置当前通道
    // 根据channelId找到对应的按钮并设置为选中状态
    foreach (QAbstractButton *btn, channelButtonGroup->buttons()) {
        if (btn->text() == channelId) {
            btn->setChecked(true);
            break;
        }
    }
}

QString BottomStatusWidget::getCurrentChannel() const
{
    // 获取当前通道
    // 返回当前选中的通道按钮的文本
    QAbstractButton *checkedButton = channelButtonGroup->checkedButton();
    if (checkedButton) {
        return checkedButton->text();
    }
    return "";
}

void BottomStatusWidget::onChannelButtonClicked(int id)
{
    // 通道按钮被点击
    // 更新按钮状态并发出channelSelected信号
    QAbstractButton *clickedButton = channelButtonGroup->button(id);
    if (clickedButton) {
        emit channelSelected(clickedButton->text());
    }
}

void BottomStatusWidget::onMuteToggled(bool checked)
{
    // 静音状态切换
    // 发出muteStateChanged信号
    emit muteStateChanged(checked);
}

void BottomStatusWidget::onFullScreenToggled(bool checked)
{
    // 全屏状态切换
    // 发出fullScreenStateChanged信号
    emit fullScreenStateChanged(checked);
}
